{include file="../application/core/view/common/mobile/header.html" title="$pageTitle"/}
<script type="text/javascript" src="__JS__/date/date-zh-CN.js"></script>

<!-- 自定义区块:表格前面的内容 -->
{block name="pretable"}
{/block}

<!-- 表格区域 -->
<div class="mobile-table" id="mobileDataTable"></div>

<!-- 自定义区块:表格后面的内容 -->
{block name="posttable"}
{/block}

<!-- 表格配置 -->
{php} $__table_config_json = json_encode($__table_config);{/php}
<script>
    var __table_config = {$__table_config_json};
    console.log(__table_config);
</script>

<!-- 自定义区块:表格初始化前的前置操作 -->
{block name="preop"}
{/block}

<script>
    (function ($) {
        /**
         * 数据请求地址
         */
        function getDataUrl() {
            var dataUrl = 'data?_=' + (new Date().getTime());
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            var ps = ["___currPage", "___pageSize", "___order", "___where"];
            for (var i = 0; i < vars.length; i++) {
                var pair = vars[i].split("=");
                if (ps.indexOf(pair[0].toLowerCase()) == -1) {
                    dataUrl += "&" + pair[0] + "=" + pair[1];
                }
            }
            return dataUrl;
        }

        /**
         * 获取表格配置
         *
         * @returns
         */
        function getTableConfig() {
            var tableConfig = {};

            // 处理每个字段列
            var cols = [];
            for (var field in __table_config.fieldsConfig) {
                var col = {};
                var fieldConfig = __table_config.fieldsConfig[field];
                if (fieldConfig.mobile === false) {
                    continue;
                }

                if (typeof fieldConfig.mobile != 'object') {
                    fieldConfig.mobile = {};
                }
                col.title = fieldConfig.mobile.title || fieldConfig.title;
                col.position = fieldConfig.mobile.position || 'detail';
                col.align = fieldConfig.mobile.align || fieldConfig.align;
                col.ignore = fieldConfig.mobile.ignore || fieldConfig.ignore;
                col.hide = fieldConfig.mobile.hide || fieldConfig.hide;
                col.type = fieldConfig.mobile.type || fieldConfig.type;
                col.templet = fieldConfig.mobile.templet || fieldConfig.templet;
                col.decimalPlaces = fieldConfig.mobile.decimalPlaces || fieldConfig.decimalPlaces;
                col.format = fieldConfig.mobile.format || fieldConfig.format;
                col.detailWidth = fieldConfig.mobile.detailWidth;

                if (!col.templet) {
                    if (col.type == 'number') {
                        //数字
                        col.templet = function (d) {
                            if (this.decimalPlaces) {
                                return d[this.field].toFixed(this.decimalPlaces);
                            }
                            return d[this.field];
                        }
                    } else if (col.type == 'percent') {
                        //百分比
                        col.templet = function (d) {
                            if (this.decimalPlaces) {
                                return (d[this.field] * 100).toFixed(this.decimalPlaces);
                            }
                            return parseInt(d[this.field] * 10000) / 100 + "%";
                        }
                    } else if (col.type == 'image') {
                        //图片字段
                        col.templet = function (d) {
                            return '<img class="cell-img" src="' + d[this.field] + '"/>';
                        }
                    } else if (col.type == 'timestamp') {
                        if (typeof col.format == 'undefined') {
                            col.format = 'yyyy-MM-dd HH:mm:ss';
                        }
                        //时间戳字段
                        col.templet = function (d) {
                            var dt = new Date(d[this.field] * 1000);
                            return dt.toString(this.format);
                        }
                    } else if (col.type == 'cdu') {
                        //cdu组件
                        col.templet = function (d) {
                            if (!d[this.field] || d[this.field] == null) {
                                return "";
                            }
                            try {
                                var belong = eval("(" + d[this.field] + ")");
                                var names = [];
                                $.each(belong, function () {
                                    names.push(this.name);
                                });
                                return names.join(", ");
                            } catch (e) {
                                return d[this.field];
                            }
                        }
                    } else if (col.type == 'bool') {
                        //bool
                        col.templet = function (d) {
                            return d[this.field] ? "是" : "否";
                        }
                    }
                }

                cols[field] = col;
            }
            tableConfig.cols = cols;

            tableConfig.rowHeight = __table_config.rowHeight;
            tableConfig.majorRowHeight = __table_config.majorRowHeight;

            tableConfig.url = getDataUrl();

            return tableConfig;
        }

        //config的设置是全局的
        layui.config({
            base: '/static/layui/mods' //假设这是你存放拓展模块的根目录
        }).extend({
            mobileTable: '/mobileTable/mobileTable'
        });

        layui.use(['mobileTable'], function () {
            var mobileTable = layui.mobileTable;
            mobileTable.render("mobileDataTable", getTableConfig());
        });
    })(jQuery);
</script>

<!-- 自定义区块:表格初始化前的后置操作 -->
{block name="postop"}
{/block}

{include file="../application/core/view/common/mobile/footer.html"/}